package com.huawei.hms.support.api.hwid;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import com.huawei.appgallery.serverreqkit.api.bean.startup.StartupResponse;
import com.huawei.hms.api.Api.ApiOptions;
import com.huawei.hms.api.HuaweiApiAvailability;
import com.huawei.hms.api.HuaweiApiClient;
import com.huawei.hms.api.HuaweiApiClientImpl;
import com.huawei.hms.auth.api.signin.HuaweiIdSignIn;
import com.huawei.hms.auth.api.signin.HuaweiIdSignInResult;
import com.huawei.hms.auth.api.signin.internal.i;
import com.huawei.hms.common.api.OptionalPendingResult;
import com.huawei.hms.common.api.internal.OptionalPendingResultImpl;
import com.huawei.hms.core.aidl.IMessageEntity;
import com.huawei.hms.support.api.PendingResultImpl;
import com.huawei.hms.support.api.client.PendingResult;
import com.huawei.hms.support.api.client.Result;
import com.huawei.hms.support.api.client.Status;
import com.huawei.hms.support.api.entity.auth.PermissionInfo;
import com.huawei.hms.support.api.entity.auth.Scope;
import com.huawei.hms.support.api.entity.hwid.HwIDConstant.RETCODE;
import com.huawei.hms.support.api.entity.hwid.HwIDConstant.RETKEY;
import com.huawei.hms.support.api.entity.hwid.HwIDNaming;
import com.huawei.hms.support.api.entity.hwid.QueryShippingAddressReq;
import com.huawei.hms.support.api.entity.hwid.SignInReq;
import com.huawei.hms.support.api.entity.hwid.SignInResp;
import com.huawei.hms.support.api.entity.hwid.SignOutReq;
import com.huawei.hms.support.log.HMSLog;
import com.huawei.hms.utils.Checker;
import com.huawei.hwid.core.constants.HwAccountConstants;
import java.lang.ref.WeakReference;
import java.security.SecureRandom;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Timer;
import o.fjo;

public class HuaweiIdApiImpl implements HuaweiIdApi {
    private HuaweiApiClient a = null;
    private WeakReference<Activity> b = null;

    static List<Scope> a(HuaweiApiClient huaweiApiClient) {
        return ((HuaweiApiClientImpl) huaweiApiClient).getScopes();
    }

    static List<PermissionInfo> b(HuaweiApiClient huaweiApiClient) {
        return ((HuaweiApiClientImpl) huaweiApiClient).getPermissionInfos();
    }

    public PendingResult<AddressResult> queryShippingAddress(HuaweiApiClient huaweiApiClient, SignInHuaweiId signInHuaweiId) {
        HMSLog.i("HuaweiIdApiImpl", "Enter queryShippingAddress");
        return new b(huaweiApiClient, HwIDNaming.queryShippingAddress, new QueryShippingAddressReq(signInHuaweiId));
    }

    public PendingResult<SignInResult> signIn(Activity activity, HuaweiApiClient huaweiApiClient) {
        HMSLog.i("HuaweiIdApiImpl", "Enter signIn");
        Checker.checkNonNull(activity, "Activity must not be null.");
        Checker.checkNonNull(huaweiApiClient, "HuaweiApiClient must not be null.");
        List<Scope> a = a(huaweiApiClient);
        List<PermissionInfo> b = b(huaweiApiClient);
        Set hashSet = new HashSet();
        for (Scope scopeUri : a) {
            hashSet.add(scopeUri.getScopeUri());
        }
        Set hashSet2 = new HashSet();
        for (PermissionInfo permission : b) {
            hashSet2.add(permission.getPermission());
        }
        IMessageEntity signInReq = new SignInReq(hashSet, hashSet2);
        this.a = huaweiApiClient;
        this.b = new WeakReference(activity);
        return new 1(this, huaweiApiClient, HwIDNaming.signin, signInReq);
    }

    public PendingResult<SignInResult> signInBackend(HuaweiApiClient huaweiApiClient) {
        HMSLog.i("HuaweiIdApiImpl", "Enter signInBackend");
        Checker.checkNonNull(huaweiApiClient, "HuaweiApiClient must not be null.");
        this.a = huaweiApiClient;
        List<Scope> a = a(huaweiApiClient);
        List<PermissionInfo> b = b(huaweiApiClient);
        Set hashSet = new HashSet();
        for (Scope scopeUri : a) {
            hashSet.add(scopeUri.getScopeUri());
        }
        Set hashSet2 = new HashSet();
        for (PermissionInfo permission : b) {
            hashSet2.add(permission.getPermission());
        }
        return new PendingResultImpl<SignInResult, SignInResp>(this, huaweiApiClient, HwIDNaming.singinbackend, new SignInReq(hashSet, hashSet2)) {
            final /* synthetic */ HuaweiIdApiImpl a;

            public /* synthetic */ Result onComplete(IMessageEntity iMessageEntity) {
                return a((SignInResp) iMessageEntity);
            }

            public SignInResult a(SignInResp signInResp) {
                HMSLog.i("HuaweiIdApiImpl", "signInBackend onComplete:" + signInResp.getRetCode());
                Intent data = signInResp.getData();
                data.putExtra(RETKEY.RETCODE, signInResp.getRetCode());
                return this.a.getHwIdSignInResultFromIntent(data);
            }
        };
    }

    public PendingResult<Status> signOut(HuaweiApiClient huaweiApiClient) {
        HMSLog.i("HuaweiIdApiImpl", "Enter signOut");
        if (huaweiApiClient.isConnected()) {
            return new d(huaweiApiClient, HwIDNaming.signout, new SignOutReq());
        }
        return d(huaweiApiClient);
    }

    public SignInResult getHwIdSignInResultFromIntent(Intent intent) {
        SignInResult signInResult;
        if (intent != null) {
            Bundle extras = intent.getExtras();
            if (extras != null) {
                int i = extras.getInt(RETKEY.RETCODE, 0);
                if (i == 0) {
                    String string = extras.getString(RETKEY.GENDER);
                    String string2 = extras.getString(RETKEY.USERID, "");
                    String string3 = extras.getString(RETKEY.STATUS);
                    String string4 = extras.getString(RETKEY.SCOPE);
                    String string5 = extras.getString(RETKEY.ID_TOKEN);
                    String string6 = extras.getString(RETKEY.EMAIL);
                    String string7 = extras.getString(RETKEY.FAMILY_NAME);
                    String string8 = extras.getString(RETKEY.GIVEN_NAME);
                    SignInHuaweiId build = SignInHuaweiId.build(extras.getString(RETKEY.OPENID, ""), string2, extras.getString(RETKEY.DISPLAYNAME, ""), extras.getString(RETKEY.PHOTOURL), extras.getString(RETKEY.ACCESS_TOKEN, ""), extras.getString(RETKEY.SERVICECOUNTRYCODE, ""), conversionStatus(string3), conversionGender(string), conversionScopes(string4), extras.getString(RETKEY.SERVICEAUTHCODE, ""), extras.getString(RETKEY.UNIONID, ""), extras.getString(RETKEY.COUNTRYCODE, ""));
                    build.setIdToken(string5);
                    build.setEmail(string6);
                    build.setFamilyName(string7);
                    build.setGivenName(string8);
                    Status status = new Status(i);
                    signInResult = new SignInResult(status);
                    signInResult.setStatus(status);
                    signInResult.setSignInHuaweiId(build);
                    HMSLog.i("HuaweiIdApiImpl", "getSignInResult success");
                    return signInResult;
                }
                PendingIntent pendingIntent = null;
                if (i == 2002 || i == 2004) {
                    pendingIntent = PendingIntent.getActivity(this.a.getContext(), new SecureRandom().nextInt(), intent, 0);
                }
                Status status2 = new Status(i, null, pendingIntent);
                signInResult = new SignInResult(status2);
                signInResult.setStatus(status2);
                signInResult.setData(intent);
                HMSLog.i("HuaweiIdApiImpl", "getSignInResult fail");
                return signInResult;
            }
        }
        signInResult = new SignInResult(new Status(RETCODE.SIGN_IN_PARAMS_ERROR));
        HMSLog.e("HuaweiIdApiImpl", "getSignInResult error");
        return signInResult;
    }

    public HuaweiIdSignInResult getHuaweiIdSignInResultFromIntent(Intent intent) {
        Status status;
        HuaweiIdSignInResult huaweiIdSignInResult = new HuaweiIdSignInResult();
        if (intent != null) {
            Bundle extras = intent.getExtras();
            if (extras != null) {
                int i = extras.getInt(RETKEY.RETCODE, 0);
                status = new Status(i);
                if (i == 0) {
                    huaweiIdSignInResult.setSignInHuaweiId(a(extras));
                    HMSLog.i("HuaweiIdApiImpl", "getSignInResult success");
                } else {
                    HMSLog.i("HuaweiIdApiImpl", "getSignInResult fail");
                }
                huaweiIdSignInResult.setStatus(status);
                return huaweiIdSignInResult;
            }
        }
        status = new Status(RETCODE.SIGN_IN_PARAMS_ERROR);
        HMSLog.e("HuaweiIdApiImpl", "getSignInResult error");
        huaweiIdSignInResult.setStatus(status);
        return huaweiIdSignInResult;
    }

    private SignInHuaweiId a(Bundle bundle) {
        int parseInt;
        String string = bundle.getString(RETKEY.ACCESS_TOKEN, "");
        String string2 = bundle.getString(RETKEY.SERVICEAUTHCODE, "");
        String string3 = bundle.getString(RETKEY.DISPLAYNAME, "");
        String string4 = bundle.getString(RETKEY.GENDER);
        if (TextUtils.isEmpty(string4)) {
            string4 = StartupResponse.CHANNELNO_QUERY_FAILURE;
        }
        try {
            parseInt = Integer.parseInt(string4);
        } catch (NumberFormatException e) {
            parseInt = -1;
        }
        String string5 = bundle.getString(RETKEY.USERID, "");
        string4 = bundle.getString(RETKEY.OPENID, "");
        String string6 = bundle.getString(RETKEY.PHOTOURL);
        String string7 = bundle.getString(RETKEY.SERVICECOUNTRYCODE, "");
        String string8 = bundle.getString(RETKEY.COUNTRYCODE, "");
        String string9 = bundle.getString(RETKEY.UNIONID, "");
        int a = a(bundle.getString(RETKEY.STATUS));
        String string10 = bundle.getString(RETKEY.SCOPE);
        Set hashSet = new HashSet();
        if (string10 != null) {
            for (String scope : string10.split(HwAccountConstants.BLANK)) {
                hashSet.add(new Scope(scope));
            }
        }
        SignInHuaweiId build = SignInHuaweiId.build(string4, string5, string3, string6, string, string7, a, parseInt, hashSet, string2, string9, string8);
        build.setIdToken(bundle.getString(RETKEY.ID_TOKEN));
        build.setEmail(bundle.getString(RETKEY.EMAIL));
        build.setFamilyName(bundle.getString(RETKEY.FAMILY_NAME));
        build.setGivenName(bundle.getString(RETKEY.GIVEN_NAME));
        return build;
    }

    private int a(String str) {
        if (TextUtils.isEmpty(str)) {
            str = "0";
        }
        try {
            return Integer.parseInt(str);
        } catch (NumberFormatException e) {
            return 0;
        }
    }

    public HuaweiIdSignInResult getSignInResultFromIntent(Intent intent) {
        return i.a(intent);
    }

    public Intent getSignInIntent(HuaweiApiClient huaweiApiClient) {
        String str = null;
        Context context = huaweiApiClient.getContext();
        HuaweiIdSignInOptions signInOption = getSignInOption(huaweiApiClient);
        if (signInOption == null) {
            return null;
        }
        if (huaweiApiClient.getSubAppInfo() != null) {
            str = huaweiApiClient.getSubAppInfo().getSubAppID();
        }
        return i.a(context, signInOption, str);
    }

    public PendingResult<Status> revokeAccess(HuaweiApiClient huaweiApiClient) {
        HMSLog.i("HuaweiIdApiImpl", "Enter revokeAccess");
        this.a = huaweiApiClient;
        return new c(huaweiApiClient, HwIDNaming.revokeAccess, c(huaweiApiClient));
    }

    public OptionalPendingResult<HuaweiIdSignInResult> silentSignIn(HuaweiApiClient huaweiApiClient) {
        HMSLog.i("HuaweiIdApiImpl", "Enter silentSignIn");
        Checker.checkNonNull(huaweiApiClient, "HuaweiApiClient must not be null.");
        this.a = huaweiApiClient;
        return new OptionalPendingResultImpl(new 3(this, huaweiApiClient, HwIDNaming.singinbackend, c(huaweiApiClient)));
    }

    private SignInReq c(HuaweiApiClient huaweiApiClient) {
        List<Scope> a = a(huaweiApiClient);
        List<PermissionInfo> b = b(huaweiApiClient);
        Set hashSet = new HashSet();
        for (Scope scopeUri : a) {
            hashSet.add(scopeUri.getScopeUri());
        }
        Set hashSet2 = new HashSet();
        for (PermissionInfo permission : b) {
            hashSet2.add(permission.getPermission());
        }
        return new SignInReq(hashSet, hashSet2);
    }

    private void a(int i) {
        if (i == 0 && HuaweiApiAvailability.getInstance().isHuaweiMobileNoticeAvailable(this.a.getContext()) == 0) {
            new Timer().schedule(new 4(this), 200);
        }
    }

    protected HuaweiIdSignInOptions getSignInOption(HuaweiApiClient huaweiApiClient) {
        Map apiMap = huaweiApiClient.getApiMap();
        if (apiMap != null) {
            ApiOptions apiOptions = (ApiOptions) apiMap.get(HuaweiId.SIGN_IN_API);
            if (apiOptions != null && (apiOptions instanceof HuaweiIdSignInOptions)) {
                return (HuaweiIdSignInOptions) apiOptions;
            }
        }
        return null;
    }

    private PendingResult<Status> d(HuaweiApiClient huaweiApiClient) {
        HMSLog.i("HuaweiIdApiImpl", "Enter signOutNoConnected");
        fjo signOut = HuaweiIdSignIn.getClient(huaweiApiClient.getContext(), getSignInOption(huaweiApiClient)).signOut();
        PendingResult signOutNoConnectPendingResult = new SignOutNoConnectPendingResult(huaweiApiClient, HwIDNaming.signout, new SignOutReq());
        signOut.ˏ(new SignOutOnCompleteListener((SignOutNoConnectPendingResult) signOutNoConnectPendingResult));
        return signOutNoConnectPendingResult;
    }

    protected static int conversionGender(String str) {
        if (TextUtils.isEmpty(str)) {
            str = StartupResponse.CHANNELNO_QUERY_FAILURE;
        }
        try {
            return Integer.parseInt(str);
        } catch (NumberFormatException e) {
            return -1;
        }
    }

    protected static int conversionStatus(String str) {
        if (TextUtils.isEmpty(str)) {
            str = "0";
        }
        try {
            return Integer.parseInt(str);
        } catch (NumberFormatException e) {
            return 0;
        }
    }

    public static HashSet<Scope> conversionScopes(String str) {
        HashSet<Scope> hashSet = new HashSet();
        if (str != null) {
            for (String scope : str.split(HwAccountConstants.BLANK)) {
                hashSet.add(new Scope(scope));
            }
        }
        return hashSet;
    }
}
